package com.unina.is2.astafantaunina.activity;

import com.unina.is2.astafantaunina.R;
import com.unina.is2.astafantaunina.activity.LoggedActivity.FragmentAstaAmministra;
import com.unina.is2.astafantaunina.activity.LoggedActivity.FragmentAstaUtente;
import com.unina.is2.astafantaunina.chat.XMPP;
import com.unina.is2.astafantaunina.utility.ButtonState;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentAsta extends Fragment {
	
	private final static String MY_PREFERENCES = "UtenteAstaFantaunina";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		ButtonState buttonState = ButtonState.getButtonState();
		
		SharedPreferences sharedPref = getActivity().getSharedPreferences(MY_PREFERENCES,Context.MODE_PRIVATE);
		int i = sharedPref.getInt("indice_lega", -1);
		
		android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
		
		Fragment newFragment2 = new FragmentAstaUtente();
		transaction.add(R.id.containerAsta, newFragment2);
		
		//Se sono amministratore
		if (buttonState.getIsAmministratore(i)){
			
			Fragment newFragment = new FragmentAstaAmministra();
			transaction.add(R.id.containerAsta, newFragment);
			
		}
         
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        
        
        transaction.addToBackStack(null);
         
        // Commit the transaction
        transaction.commit();
		
		return inflater.inflate(R.layout.fragment_asta, null);
	}
	
	@Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);
        
        System.out.println("FRAGMENT ASTA SONO "+visible);
        
        ButtonState buttonState = ButtonState.getButtonState();
        
        buttonState.setIsActive(visible, 0);
        
        if (visible){
        	SharedPreferences sharedPref = LoggedActivity.getContext().getSharedPreferences(MY_PREFERENCES,Context.MODE_PRIVATE);

        	String username = sharedPref.getString("username", "");
        	
        	String password = sharedPref.getString("password", "");
        	
        	// invio un intent che setta gli utenti connessi
        	Intent i2 = new Intent("LOCATION_UPDATED");
        	i2.putExtra("<lista_connessi>",buttonState.getUtentiConnessi(0));
	        i2.putExtra("<messaggio>", "8");
	        LoggedActivity.getContext().sendBroadcast(i2);
        	
		    //Invio messaggio 0
			 String body_string = "0-"+username;		  	 
	   	     XMPP xmpp = XMPP.getSingletonXMPP(username, password); 	
	   	     
	   	     System.out.println("MESSAGGIO 0 INVIATO FRAGMENT ASTA");
	   	     
			 xmpp.sendXMPPmsg(body_string, sharedPref.getString("lega_scelta", "nessuna lega")+"@fantaunina");
        }
        
	}
	
	
}
